package com.education.service.edu.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.education.result.R;
import com.education.service.edu.entity.Teacher;
import com.education.service.edu.entity.query.TeacherQuery;
import com.education.service.edu.feign.OssFileService;
import com.education.service.edu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Lance
 * @since 2020-06-16
 */
@Api(tags = "讲师管理")
@Slf4j
@RestController
@RequestMapping("/admin/edu/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private OssFileService ossFileService;

    /**
     * 获取所有讲师的信息
     * @return
     */
    @ApiOperation("获取所有讲师的信息")
    @GetMapping("list")
    public R getListAll(){
        log.info("info - 获取所有的讲师列表");
        log.debug("debug - 获取所有的讲师列表");
        log.error("error - 获取所有的讲师列表");
        log.warn("warn - 获取所有的讲师列表");
        List<Teacher> list = teacherService.list();
        return R.ok().data("items", list).message("获取讲师列表成功!");  // 返回自定义的状态码
    }

    /**
     * 根据 ID 逻辑删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除讲师", notes = "根据 ID 逻辑删除")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam(value = "讲师 ID", required = true) @PathVariable String id){
        // 删除讲师头像
        teacherService.removeAvatarById(id);
        // 删除讲师
        boolean remove = teacherService.removeById(id);
        if (remove){
            return R.ok().message("删除成功!");
        }else{
            return R.error().message("非常抱歉,数据不存在");
        }
    }

    /**
     * 批量删除讲师
     * @param idList
     * @return
     */
    @ApiOperation("根据id列表删除讲师")
    @DeleteMapping("batch-remove")
    public R removeRows( @ApiParam(value = "讲师id列表", required = true) @RequestBody List<String> idList){
        boolean result = teacherService.removeByIds(idList);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

    /**
     * 讲师分页查询显示
     * @param page 查询第几页
     * @param limit 每页记录数
     * @return
     */
    @ApiOperation("分页讲师列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                      @ApiParam(value = "每页显示记录数", required = true) @PathVariable Long limit,
                      @ApiParam("讲师列表查询对象") TeacherQuery teacherQuery){

        Page<Teacher> pageParam  = teacherService.selectPage(page, limit, teacherQuery);
        return R.ok().data("pageParam", pageParam);
    }


    @ApiOperation("新增讲师")
    @PostMapping("save")
    public R save(@ApiParam(value = "讲师对象", required = true) @RequestBody Teacher teacher){
        boolean result = teacherService.save(teacher);
        if (result){
            return R.ok().message("添加成功!!");
        }else{
            return R.error().message("添加失败!!");
        }
    }
    @ApiOperation("修改讲师")
    @PutMapping("update")
    public R update(@ApiParam(value = "讲师对象", required = true) @RequestBody Teacher teacher){
        boolean result = teacherService.updateById(teacher);
        if (result){
            return R.ok().message("添加成功!!");
        }else{
            return R.error().message("添加失败!!");
        }
    }

    @ApiOperation("根据ID查询讲师")
    @GetMapping("getById/{id}")
    public R getById(@ApiParam(value = "讲师ID", required = true) @PathVariable String id){

        // 查询讲师信息
        Teacher teacher = teacherService.getById(id);
        if (teacher != null){
            return R.ok().data("items", teacher);
        }else {
            return R.error().message("数据不存在");
        }
    }

    /**
     * 查询的时候根据关键字快速响应提示
     * @param key
     * @return
     */
    @ApiOperation("根据左关键字查询讲师名列表")
    @GetMapping("list/name/{key}")
    public R selectNameListByKey(
            @ApiParam(value = "查询关键字", required = true)
            @PathVariable String key){

        List<Map<String, Object>> nameList = teacherService.selectNameListByKey(key);

        return R.ok().data("nameList", nameList);
    }


    /**
     * 测试 OpenFeign
     * @return
     */
    @ApiOperation("测试微服务远程调用")
    @GetMapping("test")
    public R testOpenFeign(){
        ossFileService.test();
        return R.ok();
    }
}

